
#include <stdio.h>
#include "dina_decoder.h"

void testDecode();

#if defined(__EMSCRIPTEN__)
EM_JS(void, jsVideoCallback, (unsigned char * buff, int size, int w, int h), {
    console.log("js videoCallback  bufsize=" + size);
    // var outArray = Module.HEAPU8.subarray(buff, buff + size);
    // var data = new Uint8ClampedArray(outArray); //Uint8Array
    // var o = {
    //     state:DecodeStateSucc,
    //     data:data
    // }
    // self.postMessage(o, [o.data]);
    jsvc2(  buff,   size,   w,   h);
    // var canvas = document.getElementById('myCanvas');
    // var ctx = canvas.getContext('2d');
    // var imgData = ctx.createImageData(w, h);
    // for (var i = 0, j = 0; i < imgData.data.length; i += 4, j += 3)
    // {
    //     //之所以这样是因为canvas只接受rgba数据，而我们只有rgb,因此最后一个直接为255即可
    //     imgData.data[i + 0] = data[j + 0];
    //     imgData.data[i + 1] = data[j + 1];
    //     imgData.data[i + 2] = data[j + 2];
    //     imgData.data[i + 3] = 255; 
    // }
    // ctx.putImageData(imgData, 0, 0); //后边的两个参数是渲染的起始位置，这里就设为（0，0）了，表示左上角
});

#endif

int main(int argc, char *argv[])
{
    printf("decodetest\n");
    //直接执行不绘制canvas
    testDecode(0);
}
//测试程序的视频回调
static void testVideoCallback(unsigned char *buff, int size, int frameWidth, int frameHeight)
{
    static int iFrameCount = 0;
    char outFIleName[256];

    debug("testVideoCallback:imgsize=%d,pix_w=%d,frameHeight=%d", size, frameWidth, frameHeight);
    iFrameCount++;

#if defined(__EMSCRIPTEN__)
    { //wasm环境
        jsVideoCallback(buff, size, frameWidth, frameHeight);
    }
#else
    {
        //非wasm环境
        if (iFrameCount >= 5)
        {
            return;
        }
        sprintf(outFIleName, "test%d.RGB", iFrameCount);

        FILE *outFile = fopen(outFIleName, "wb");
        char *pOutData = buff;
        int outSize = size;
        fwrite(pOutData, 1, outSize, outFile);
        fclose(outFile);
    }
#endif
}

void testDecode()
{
    long videodecoder = initH264Decoder((long)testVideoCallback, 0,OUT_IMG_RGB);
    unsigned char h264Buf[1024 * 512];
    int h264Lenth = 0;
    char frame_buf[1024] = {0};
    FILE *inFile = fopen("wasm-test/test.h264", "rb");

    //FILE * InFile = fopen("bigbuckbunny_480x272.h265", "rb");

    while (true)
    {
        int iReadSize = fread(frame_buf, 1, 512, inFile);
        if (iReadSize <= 0)
        {
            break;
        }
        memcpy(h264Buf + h264Lenth, frame_buf, iReadSize);
        h264Lenth += iReadSize;
        //获取一帧数据
        while (true)
        {
            bool oneFrame = false;
            if (h264Lenth <= 8)
            {
                break;
            }
            for (int i = 4; i < h264Lenth - 4; i++)
            {
                if (h264Buf[i] == 0x00 && h264Buf[i + 1] == 0x00 && h264Buf[i + 2] == 0x00 && h264Buf[i + 3] == 0x01)
                {
                    addRawVideoFrameData(videodecoder, h264Buf, i,0,0);
                    h264Lenth -= i;
                    memcpy(h264Buf, h264Buf + i, h264Lenth);
                    oneFrame = true;
                    break;
                }
            }
            if (oneFrame)
            {
                continue;
            }
            else
            {
                break;
            }
        }
    }
}